package com.none.core.common.utils;

import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.servlet.http.HttpServletRequest;

public class PathUtil {
	/**
	 * 获取tomcat Webapps路径
	 * @param request
	 * @return
	 */
	public static String getServerWebappPath(HttpServletRequest request){
		String tomcatRoot = request.getSession().getServletContext().getRealPath("/");
		String[] foo = tomcatRoot.split("/");  
        StringBuilder tomcatWebAppsBuilder = new StringBuilder();  
        int i = 0;  
        for(String paths : foo){  
            ++i;  
            if(i != foo.length){  
                tomcatWebAppsBuilder.append(paths);  
                tomcatWebAppsBuilder.append("/");  
            }  
        } 
        //System.out.println("^^^^^^^^^^   "+tomcatWebAppsBuilder.toString());
        return tomcatWebAppsBuilder.toString();
	}
	
	public static String getServerPath(HttpServletRequest request){
		 String addr = request.getScheme()+"://"+request.getLocalAddr()+":"+request.getLocalPort();     
//         try {  
//             addr = InetAddress.getLocalHost().getHostAddress();  
//         } catch (UnknownHostException e) {  
//             e.printStackTrace();  
//         } 
         //System.out.println("+++++++++++ "+addr+":"+request.getServerPort());
		 addr = addr.replace("https", "http");
	     return addr;
	}
}
